const Payment = require('wechat-pay').Payment;
import { config } from './../utils/config';

// import fs from 'fs';
// import { resolve } from 'path';

export default class WechatPay {
  initConfig: any;
  payment: any;

  constructor(params) {
    // console.log( `${configure.server.backServer}/wechat/WechatUpdateOrderWechat?out_order_sn=${params.out_trade_no}&order_sn=${params.yuanOrder_sn}&userid=${params.userid}`)
    this.initConfig = {
      partnerKey: params.wx_key, //商户秘钥key
      appId: params.wx_appid, //公众号appid
      mchId: params.wx_mchid, //商户秘钥
      notifyUrl: `${config.baseUrl}/wechat/selectOrderStatus`,
      // pfx:fs.readFileSync(resolve(__dirname + "./../server/static/apiclient_cert.p12"))
    };

    this.payment = new Payment(this.initConfig);
  }

  async createOrder(orderParams) {
    const data = {
      attach: orderParams.attach,
      body: orderParams.body,
      openid: orderParams.openid,
      out_trade_no: orderParams.out_trade_no,
      spbill_create_ip: orderParams.spbill_create_ip,
      total_fee: orderParams.total_fee,
      trade_type: orderParams.trade_type,
    };

    return new Promise((resolve: any, reject) => {
      this.payment.getBrandWCPayRequestParams(
        data,
        (err: any, payargs: any) => {
          // console.log(err);
          // console.log(payargs);
          if (!err) {
            resolve(payargs);
          } else {
            reject(err);
          }
        }
      );
    });
  }
}
